//
//  Ext+UITabBarItem.swift
//  JJSina
//
//  Created by mac on 2017/11/30.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    convenience init(imageName: String? = nil, title: String? = nil, target: Any?, action: Selector) {
        
        self.init()
        // 创建按钮
        let btn = UIButton()
        // 添加监听事件
        btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        // 添加图片
        if let img = imageName {
            btn.setImage(UIImage(named: img), for: UIControlState.normal)
            btn.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        if let tit = title {
            btn.setTitle(tit, for: UIControlState.normal)
            btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            btn.setTitleColor(JJ_THEMECOLOR, for: UIControlState.highlighted)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        }
        
        btn.sizeToFit()
        customView = btn
    }
}
